using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using TVDesigner.GUI.Elements;
using TVDesigner.Entities.Elements;

namespace TVDesigner.GUI.Controls
{
    public partial class DiagramPanel : UserControl
    {
        #region Atributos
        private static int diagramPanelCount = 0;

        private FMain _MainForm;
        private TVDesigner.Entities.Elements.Screen _Screen; 
        #endregion

        #region Propriedades

        public TVDesigner.Entities.Elements.Screen Screen
        {
            get
            {
                return _Screen;
            }
            set { _Screen = value; }
        }

        public DesignPanel DesignPanel
        {
            get { return _DesignPanel; }
        }

        public FMain MainForm
        {
            get { return _MainForm; }
            set
            {
                _DesignPanel.MainForm = value;
                _CodePanel.MainForm = value;
                _MainForm = value;
            }
        }

        public Element FocusElement
        {
            get
            {
                return _DesignPanel.FocusElement;
            }
        } 

        public List<Element> Elements
        {
            get
            {
                return _DesignPanel.Elements;
            }
        } 
        #endregion

        public DiagramPanel()
            : this(null, null)
        {
        }

        public DiagramPanel(FMain mainForm, TVDesigner.Entities.Elements.Screen screen)
        {
            InitializeComponent();

            _MainForm = mainForm;

            _DesignPanel.Diagram = this;
            _DesignPanel.MainForm = _MainForm;

            if (screen == null)
            {
                this.Name = "diagramPanel" + diagramPanelCount++;
                _Screen = new TVDesigner.Entities.Elements.Screen();
                _Screen.Name = this.Name;
            }
            else
            {                
                _Screen = screen;
                this.Name = screen.Name;
                _CreateElements();
            }
        }

        private void _CreateElements()
        {
            foreach (GenericComponent gc in _Screen.Components)
            {
                Element element = _CreateElement(gc);
                this.AddElement(element, false);
            }
        }

        private Element _CreateElement(GenericComponent gc)
        {
            Element element = null;
            if (gc is TVDesigner.Entities.Elements.HButton)
            {
                element = new ButtonElement(gc as HButton);
            }
            else if (gc is TVDesigner.Entities.Elements.HIcon)
            {
                element = new PictureElement(gc as HIcon);
            }
            else if (gc is TVDesigner.Entities.Elements.HText)
            {
                element = new TextElement(gc as HText);
            }
            else if (gc is TVDesigner.Entities.Elements.ScreenResize)
            {
                element = new ScreenResizeElement(gc as ScreenResize);
            }

            return element;
        }

        public void AddElement(Element element, bool hasFocus)
        {
            _DesignPanel.AddElement(element, hasFocus);
        }

        public void RemoveElement(Element element)
        {
            _DesignPanel.RemoveElement(element);
        }

        public void RemoveSelectedElement()
        {
            _DesignPanel.RemoveSelectedElement();
        }
    }
}
